<?php
namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Registration;

/**
 * 报名表接口
 */
class RegistrationForm extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 获取报名表设置
     *
     * @param int $courses 选中的课程ID数组
     * @param int $user_id 用户ID（可选）
     * @return void
     */
    public function getFormSettings()
    {
        //获取以json数据传送过来的数据
        $courses = $this->request->post('courses/a');
        $user = $this->auth->getUser(true); // 获取用户ID，默认为0
        $user_id = $user&&$user['uid'] ? $user['uid'] : 0;
        $classes_id=$courses[0]["classes_id"];
        if (empty($courses)) {
            $this->error('参数错误');
        }

        if($this->request->post("apply_form_id")&&is_numeric($this->request->post("apply_form_id"))){
            $apply_form_id = $this->request->post("apply_form_id");
            $apply_form_ids[] = ["apply_form_id"=>$apply_form_id];
        }else if($this->request->post("table_id")&&is_numeric($this->request->post("table_id"))){
            $table_id = $this->request->post("table_id");
            $apply_form_ids = Db::name('eb_apply_form')->field("id as apply_form_id")->where("table_id","=", $table_id)->group("id")->select();
        }else {

            $formSettings = Db::name('fa_course_table_autoplan');

            $formSettings->where(function ($query) use ($courses) {
                foreach ($courses as $course) {
                    $query->whereOr(function ($q) use ($course) {
                        $q->where([
                            'classes_id' => $course['classes_id'],
                            'table_type' => $course['classes_type']
                        ])->whereOr('apply_form_id', $course['apply_form_id']);
                    });
                }
            });
            $apply_form_ids = $formSettings->field("apply_form_id")->group("apply_form_id")->select();
        }
        if (empty($apply_form_ids)) {
            if( $this->request->post("apply_form_id")){
                $apply_form_ids[0]["apply_form_id"]=$this->request->post("apply_form_id");
            }else {
                $this->error('没有找到报名表设置');
            }
        }
        if(isset($apply_form_ids[0])&&$apply_form_ids[0]["apply_form_id"]==0){
            $lists=$this->getTableReportByClassesId($classes_id);
            $apply_form=db::name("eb_apply_form")->where("table_id","=", $lists["table_id"])->find();
            if($apply_form){
                $apply_form_ids[0]["apply_form_id"]=$apply_form["id"];
            }
        }

        // 获取报名表设置
        $apply_form_ids=array_column($apply_form_ids, 'apply_form_id');
        $form = Db::name('eb_apply_form')->where('id', 'in',$apply_form_ids)->select();
        if (!$form) {
            $coursetableQuery = db::name("fa_course_table")
                ->where("classes_id", "like", "%," . $classes_id . ",%")
                ->whereOr("classes_id", "like", "%," . $classes_id)
                ->whereOr("classes_id", "like", $classes_id . ",%")
                ->whereOr("classes_id", "=", $classes_id)
                ->find();
            $row['table_id']=$coursetableQuery["id"];
            if($row['table_id']>0) {
                db::name("eb_classes")->where("id", "=", $classes_id)->update(["table_id" => $row['table_id']]);
                $form = Db::name('eb_apply_form')->where('table_id', '=', $row['table_id'])->select();
            }
        }
        $value = Db::name('fa_registration')->where('table_id', '=',$form[0]['table_id'])->where('user_id', '=',$user_id)->find();
        // 解析必填字段
        $required_fields = [];
        foreach($form as $key=>$item){
            if (!empty($item['required_fields'])) {
                $required_fields = array_merge($required_fields,json_decode($item['required_fields'], true));
                // 排重
                $required_fields = array_unique($required_fields);
            }
        }
        
        // 确保身份证号字段始终包含在必填字段中
        if (!in_array('id_card', $required_fields)) {
            $required_fields[] = 'id_card';
        }

        // 字段映射
        $field_map = [
            'name' => ['label' => '姓名', 'type' => 'text', 'required' => true],
            'gender' => ['label' => '性别', 'type' => 'radio', 'required' => true, 'options' => [['value' => '男', 'text' => '男'], ['value' => '女', 'text' => '女']]],
            'nation' => ['label' => '民族', 'type' => 'select', 'required' => false, 'options' => [
                ['value' => '汉族', 'text' => '汉族'],
                ['value' => '壮族', 'text' => '壮族'],
                ['value' => '满族', 'text' => '满族'],
                ['value' => '回族', 'text' => '回族'],
                ['value' => '苗族', 'text' => '苗族'],
                ['value' => '维吾尔族', 'text' => '维吾尔族'],
                ['value' => '土家族', 'text' => '土家族'],
                ['value' => '彝族', 'text' => '彝族'],
                ['value' => '蒙古族', 'text' => '蒙古族'],
                ['value' => '藏族', 'text' => '藏族'],
                ['value' => '布依族', 'text' => '布依族'],
                ['value' => '侗族', 'text' => '侗族'],
                ['value' => '瑶族', 'text' => '瑶族'],
                ['value' => '朝鲜族', 'text' => '朝鲜族'],
                ['value' => '白族', 'text' => '白族'],
                ['value' => '哈尼族', 'text' => '哈尼族'],
                ['value' => '哈萨克族', 'text' => '哈萨克族'],
                ['value' => '黎族', 'text' => '黎族'],
                ['value' => '傣族', 'text' => '傣族'],
                ['value' => '畲族', 'text' => '畲族'],
                ['value' => '傈僳族', 'text' => '傈僳族'],
                ['value' => '仡佬族', 'text' => '仡佬族'],
                ['value' => '东乡族', 'text' => '东乡族'],
                ['value' => '高山族', 'text' => '高山族'],
                ['value' => '拉祜族', 'text' => '拉祜族'],
                ['value' => '水族', 'text' => '水族'],
                ['value' => '佤族', 'text' => '佤族'],
                ['value' => '纳西族', 'text' => '纳西族'],
                ['value' => '羌族', 'text' => '羌族'],
                ['value' => '土族', 'text' => '土族'],
                ['value' => '仫佬族', 'text' => '仫佬族'],
                ['value' => '锡伯族', 'text' => '锡伯族'],
                ['value' => '柯尔克孜族', 'text' => '柯尔克孜族'],
                ['value' => '达斡尔族', 'text' => '达斡尔族'],
                ['value' => '景颇族', 'text' => '景颇族'],
                ['value' => '毛南族', 'text' => '毛南族'],
                ['value' => '撒拉族', 'text' => '撒拉族'],
                ['value' => '布朗族', 'text' => '布朗族'],
                ['value' => '塔吉克族', 'text' => '塔吉克族'],
                ['value' => '阿昌族', 'text' => '阿昌族'],
                ['value' => '普米族', 'text' => '普米族'],
                ['value' => '鄂温克族', 'text' => '鄂温克族'],
                ['value' => '怒族', 'text' => '怒族'],
                ['value' => '京族', 'text' => '京族'],
                ['value' => '基诺族', 'text' => '基诺族'],
                ['value' => '德昂族', 'text' => '德昂族'],
                ['value' => '保安族', 'text' => '保安族'],
                ['value' => '俄罗斯族', 'text' => '俄罗斯族'],
                ['value' => '裕固族', 'text' => '裕固族'],
                ['value' => '乌孜别克族', 'text' => '乌孜别克族'],
                ['value' => '门巴族', 'text' => '门巴族'],
                ['value' => '鄂伦春族', 'text' => '鄂伦春族'],
                ['value' => '独龙族', 'text' => '独龙族'],
                ['value' => '塔塔尔族', 'text' => '塔塔尔族'],
                ['value' => '赫哲族', 'text' => '赫哲族'],
                ['value' => '珞巴族', 'text' => '珞巴族'],
                ['value' => '其他', 'text' => '其他']
            ]],
            'id_card' => ['label' => '身份证号', 'type' => 'text', 'required' => true],
            'birth_date' => ['label' => '出生日期', 'type' => 'date', 'required' => false],
            'birthplace' => ['label' => '籍贯', 'type' => 'text', 'required' => false],
            'political_status' => ['label' => '政治面貌', 'type' => 'select', 'required' => false, 'options' => [
                ['value' => '群众', 'text' => '群众'],
                ['value' => '中共党员', 'text' => '中共党员'],
                ['value' => '共青团员', 'text' => '共青团员'],
                ['value' => '民主党派', 'text' => '民主党派'],
                ['value' => '其他', 'text' => '其他']
            ]],
            'education' => ['label' => '文化程度', 'type' => 'select', 'required' => false, 'options' => [
                ['value' => '小学', 'text' => '小学'],
                ['value' => '初中', 'text' => '初中'],
                ['value' => '高中/中专', 'text' => '高中/中专'],
                ['value' => '大专', 'text' => '大专'],
                ['value' => '本科', 'text' => '本科'],
                ['value' => '硕士', 'text' => '硕士'],
                ['value' => '博士', 'text' => '博士'],
                ['value' => '其他', 'text' => '其他']
            ]],
            'phone' => ['label' => '手机号', 'type' => 'text', 'required' => true],
            'contact_phone' => ['label' => '其他联系电话', 'type' => 'text', 'required' => false],
            'email' => ['label' => '邮箱', 'type' => 'text', 'required' => false],
            'address' => ['label' => '现住址', 'type' => 'text', 'required' => true],
            'family_contact_name' => ['label' => '家庭联系人姓名', 'type' => 'text', 'required' => true],
            'family_contact_relation' => ['label' => '家庭联系人关系', 'type' => 'select', 'required' => true, 'options' => [
                ['value' => '配偶', 'text' => '配偶'],
                ['value' => '父亲', 'text' => '父亲'],
                ['value' => '母亲', 'text' => '母亲'],
                ['value' => '儿子', 'text' => '儿子'],
                ['value' => '女儿', 'text' => '女儿'],
                ['value' => '其他', 'text' => '其他']
            ]],
            'family_contact_phone' => ['label' => '家庭联系人电话', 'type' => 'text', 'required' => true],
            'former_work_unit' => ['label' => '原工作单位', 'type' => 'text', 'required' => false],
            'former_unit_type' => ['label' => '原单位类型', 'type' => 'select', 'required' => false, 'options' => [
                ['value' => '机关', 'text' => '机关'],
                ['value' => '事业单位', 'text' => '事业单位'],
                ['value' => '国有企业', 'text' => '国有企业'],
                ['value' => '民营企业', 'text' => '民营企业'],
                ['value' => '外资企业', 'text' => '外资企业'],
                ['value' => '其他', 'text' => '其他']
            ]],
            'former_position' => ['label' => '原职务', 'type' => 'text', 'required' => false],
            'job_level' => ['label' => '职级', 'type' => 'text', 'required' => false],
            'professional_title' => ['label' => '职称', 'type' => 'text', 'required' => false],
            'specialty' => ['label' => '特长', 'type' => 'textarea', 'required' => false],
            'medical_history' => ['label' => '疾病史', 'type' => 'textarea', 'required' => false]
        ];

        // 处理字段，设置是否必填
        $fields = [];
        foreach ($field_map as $field_key => $field_info) {
            $is_required = in_array($field_key, $required_fields);
            $field_info['required'] = $is_required;
            if($value&&array_key_exists($field_key,$value)&&$value[$field_key]!=null){
                $field_info['value'] = $value[$field_key];
            }
            $fields[$field_key] = $field_info;
        }

        // 获取用户已有的报名数据
        $existing_data = [];

        if ($user_id > 0) {
            // 查询用户最新的报名记录

            $existing_record = Db::name('fa_registration')
                ->where('user_id', $user_id)
                ->where('apply_form_id', 'in', $apply_form_ids)
                ->order('id', 'desc')
                ->find();
            //echo Db::name('fa_registration')->getlastsql();exit;
                //print_r($existing_record);exit;
            if ($existing_record) {
                // 过滤掉不需要的字段
                $exclude_fields = ['id', 'createtime', 'updatetime', 'deletetime', 'user_id', 'apply_form_id'];
                foreach ($existing_record as $key => $value) {
                    if (!in_array($key, $exclude_fields) && !is_null($value)) {
                        $existing_data[$key] = $value;
                    }
                }
            }
        }

        $this->success('获取成功', [
            'user_id'=>$user_id,
            'form_title' => $form[0]['form_title'],
            'form_description' => $form[0]['form_description'],
            'required_fields' => $required_fields,
            'fields' => $fields,
            'existing_data' => $existing_data, // 添加已有数据
        ]);
    }

    /**
     * 提交报名表
     *
     * @return void
     */
    public function submit()
    {
        $data = $this->request->post();
        $user = $this->auth->getUser(true);
        $user_id=$user["uid"];
        $classes_id=$data['courses'][0]["classes_id"];
        if($data["table_id"]){
            $table_id=intval($data["table_id"]);
        }else {
            $lists = $this->getTableReportByClassesId($classes_id);
            $table_id = $lists["table_id"];
            $apply_form_id = $lists["apply_form_id"];
        }
        $apply_form=db::name("eb_apply_form")->where("table_id","=", $table_id)->find();
        if($apply_form){
            $data['form_id']=$apply_form["id"];
        }
        // 验证必填字段
        $form_id = $data['form_id'] ?? 0;
        if (!$form_id) {
            $this->error('报名表ID不能为空');
        }

        // 获取报名表设置
        $form = Db::name('eb_apply_form')->where('id', $form_id)->find();
        if (!$form) {
            $this->error('报名表不存在');
        }

        // 解析必填字段
        $required_fields = [];
        if (!empty($form['required_fields'])) {
            $required_fields = json_decode($form['required_fields'], true);
        }

        // 获取表单数据
        $registration_data = $data['registration_data'] ?? [];

        // 验证必填字段
        foreach ($required_fields as $field) {
            if (empty($registration_data[$field])) {
                $field_map = [
                    'name' => '姓名',
                    'sex' => '性别',
                    'nation' => '民族',
                    'id_card' => '身份证号',
                    'birth_date' => '出生日期',
                    'birthplace' => '籍贯',
                    'political_status' => '政治面貌',
                    'education' => '文化程度',
                    'phone' => '手机号',
                    //'contact_phone' => '其他联系电话',
                    //'email' => '邮箱',
                    'address' => '现住址',
                    'family_contact_name' => '家庭联系人姓名',
                    'family_contact_relation' => '家庭联系人关系',
                    'family_contact_phone' => '家庭联系人电话',
                    //'former_work_unit' => '原工作单位',
                    //'former_unit_type' => '原单位类型',
                    //'former_position' => '原职务',
                    //'job_level' => '职级',
                    //'professional_title' => '职称',
                    //'specialty' => '特长',
                    //'medical_history' => '疾病史'
                ];
                if(!array_key_exists($field,$field_map)){
                    continue;
                }
                $this->error($field_map[$field] . '不能为空');
            }
            
            // 验证身份证号格式
            if ($field === 'id_card' && !empty($registration_data[$field])) {
                if (!preg_match('/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/', $registration_data[$field])) {
                    $this->error('请输入正确的身份证号码');
                }
            }
            
            // 验证手机号格式
            if (($field === 'phone' || $field === 'contact_phone' || $field === 'family_contact_phone' || $field === 'emergency_contact_phone') && !empty($registration_data[$field])) {
                if (!preg_match('/^1[3-9]\d{9}$/', $registration_data[$field])) {
                    $this->error('请输入正确的手机号码');
                }
            }
            
            // 验证邮箱格式
            if ($field === 'email' && !empty($registration_data[$field])) {
                if (!preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $registration_data[$field])) {
                    $this->error('请输入正确的电子邮箱地址');
                }
            }
        }

        // 准备保存数据
        $save_data = $registration_data;
        $save_data['apply_form_id'] = $form_id;
        $save_data['user_id'] = $user_id;
        $save_data['table_id'] = $table_id;
        $save_data['gender']=$save_data['sex'] = $save_data['gender']==="0"?"0":"1";
        $save_data['create_time'] = $save_data['createtime'] =date('Y-m-d H:i:s');
        $save_data['update_time'] = date('Y-m-d H:i:s');
        
        // 定义fa_registration表中存在的字段
        $allowed_fields = [
            'id', 'name', 'age', 'sex', 'id_card', 'introduction', 'createtime', 
            'updatetime', 'deletetime', 'user_id', 'agreed', 'signature', 'table_id', 
            'read_agree', 'gender', 'nation', 'birth_date', 'birthplace', 
            'political_status', 'education', 'phone', 'contact_phone', 'email', 
            'address', 'family_contact_name', 'family_contact_relation', 
            'family_contact_phone', 'former_work_unit', 'former_unit_type', 
            'former_position', 'job_level', 'professional_title', 'specialty', 
            'medical_history', 'apply_form_id', 'create_time', 'update_time', 
            'permanent_address', 'health_status', 'wechat'
        ];
        
        // 只保留表中存在的字段
        foreach ($save_data as $key => $value) {
            $save_data[$key]=str_replace("^","",$save_data[$key]);
            if (!in_array($key, $allowed_fields)) {
                unset($save_data[$key]);
            }
        }
        

        // 保存数据 family_contact_phone
        if($table_id!=0){
            $save_data['table_id']=$table_id;
        }
        $result = model('\app\admin\model\Registration')->insertUpdate($save_data, ['user_id', 'table_id']);
        if ($result) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }

    /**
     * 获取用户报名数据
     *
     * @return void
     */
    public function getUserRegistrationData()
    {
        $user = $this->auth->getUser(true);
        $user_id = $user['uid'];
        
        // 获取课程ID
        $courses = $this->request->post('courses/a');
        if (empty($courses)) {
            $this->error('参数错误');
        }
        
        $classes_id = $courses[0]["classes_id"];
        $lists = $this->getTableReportByClassesId($classes_id);
        $table_id = $lists["table_id"];
        
        // 查询用户报名数据
        $registration_data = Db::name('fa_registration')
            ->where('user_id', $user_id)
            ->where('table_id', $table_id)
            ->find();
            
        if (!$registration_data) {
            $this->error('未找到报名数据');
        }
        
        // 过滤掉不需要的字段
        $exclude_fields = ['id', 'createtime', 'updatetime', 'deletetime', 'user_id', 'table_id'];
        $filtered_data = [];
        foreach ($registration_data as $key => $value) {
            if (!in_array($key, $exclude_fields) && !is_null($value)) {
                $filtered_data[$key] = $value;
            }
        }
        
        $this->success('获取成功', $filtered_data);
    }
    
    /**
     * 获取用户报名数据（前端接口）
     *
     * @return void
     */
    public function getRegistrationData()
    {
        $user = $this->auth->getUser(true);
        $user_id = $user['uid'];
        
        // 获取参数
        $table_id = $this->request->get('table_id');
        $classes_id = $this->request->get('classes_id');
        
        // 如果提供了classes_id，则通过classes_id获取table_id
        if (empty($table_id) && !empty($classes_id)) {
            $lists = $this->getTableReportByClassesId($classes_id);
            $table_id = $lists["table_id"];
        }
        
        if (empty($table_id)) {
            $this->error('参数错误，请提供table_id或classes_id');
        }
        
        // 查询用户报名数据
        $registration_data = Db::name('fa_registration')
            ->where('user_id', $user_id)
            ->where('table_id', $table_id)
            ->find();
            
        if (!$registration_data) {
            // 不返回错误，而是返回空数据，让前端处理
            $this->success('获取成功', []);
        }
        
        // 获取表单字段映射
        $form = Db::name('eb_apply_form')->where('table_id', $table_id)->find();
        $field_map = [];
        if ($form && !empty($form['form_fields'])) {
            $form_fields = json_decode($form['form_fields'], true);
            if ($form_fields) {
                foreach ($form_fields as $field) {
                    if (isset($field['name']) && isset($field['label'])) {
                        $field_map[$field['name']] = $field['label'];
                    }
                }
            }
        }
        
        // 默认字段映射
        $default_field_map = [
            'name' => '姓名',
            'gender' => '性别',
            'nation' => '民族',
            'id_card' => '身份证号',
            'birth_date' => '出生日期',
            'birthplace' => '籍贯',
            'political_status' => '政治面貌',
            'education' => '文化程度',
            'phone' => '手机号',
            'contact_phone' => '其他联系电话',
            'email' => '邮箱',
            'address' => '现住址',
            'permanent_address' => '常住地址',
            'family_contact_name' => '家庭联系人姓名',
            'family_contact_relation' => '家庭联系人关系',
            'family_contact_phone' => '家庭联系人电话',
            'former_work_unit' => '原工作单位',
            'former_unit_type' => '原单位类型',
            'former_position' => '原职务',
            'job_level' => '职级',
            'professional_title' => '职称',
            'specialty' => '特长',
            'medical_history' => '疾病史',
            'health_status' => '健康状况',
            'wechat' => '微信号'
        ];
        
        // 合并字段映射
        $field_map = array_merge($default_field_map, $field_map);
        
        // 处理返回数据
        $result = [];
        foreach ($registration_data as $key => $value) {
            // 跳过系统字段
            if (in_array($key, ['id', 'createtime', 'updatetime', 'deletetime', 'user_id', 'table_id', 'apply_form_id'])) {
                continue;
            }
            
            // 处理空值
            if (is_null($value)) {
                $value = '';
            }
            
            // 添加字段标签
            $result[$key] = [
                'value' => $value,
                'label' => isset($field_map[$key]) ? $field_map[$key] : $key
            ];
        }
        
        $this->success('获取成功', $result);
    }
    
    /**
     * 获取服务协议内容
     *
     * @return void
     */
    public function getSignatureAndIntroduction()
    {
        // 获取参数
        $table_id = $this->request->get('table_id');
        $classes_id = $this->request->get('classes_id');
        $form_id = $this->request->get('form_id');
        $user = $this->auth->getUser(true);
        $user_id = $user['uid'];
        if(($table_id=="undefined"||$table_id==0)&&empty($form_id)&&$this->request->get('apply_form_id')){
            $form_id = $this->request->get('apply_form_id');
        }
        
        // 根据不同参数获取表单
        if (!empty($form_id)&&$form_id!=0&&$form_id!='undefined') {
            // 直接通过form_id获取
            $form = Db::name('eb_apply_form')->where('id', $form_id)->find();
        } elseif (!empty($table_id)&&is_numeric($table_id)&&$table_id>0) {
            // 通过table_id获取
            $form = Db::name('eb_apply_form')->where('table_id', $table_id)->find();
        } elseif (!empty($classes_id)&&is_numeric($classes_id)&&$classes_id>0) {
            // 通过classes_id获取table_id，再获取表单
            $lists = $this->getTableReportByClassesId($classes_id);
            $table_id = $lists["table_id"];
            $form = Db::name('eb_apply_form')->where('table_id', $table_id)->find();
        } else {
            // 获取默认的报名表
            $form = Db::name('eb_apply_form')->where('status', 1)->order('id', 'asc')->find();
        }
        if($user_id){
            $registration_data = Db::name('fa_registration')->where('user_id', $user_id)->where('table_id', $table_id)->find();
        }
        
        if (!$form) {
            $this->error('未找到服务协议');
        }

        // 处理富文本内容，确保HTML标签正确显示
        $signature_content = $form['signature_content'] ?? '';
        $signature_footer = $form['signature_footer'] ?? '';
        
        // 返回服务协议内容
        $this->success('获取成功', [
            'form_id' => $form['id'],
            'table_id' => $form['table_id'],
            'signature_title' => $form['signature_title'] ?? '用户服务协议',
            'signature_content' => $signature_content,
            'signature_footer' => $signature_footer,
            'registration_data'=>$registration_data,
        ]);
    }

    // 读取手签板的主题、正文和落款样式
    private function getSignatureAndIntroductionPrivate($form_id)
    {
        $form = Db::name('eb_apply_form')->where('id', $form_id)->find();
        if (!$form) {
            $this->error('报名表不存在');
        }
       //`signature_title``signature_content``signature_footer`
        return [
            'signature_title' => $form['signature_title'],
            'signature_content' => $form['signature_content'],
            'signature_footer' => $form['signature_footer'],
        ];
    }

    private function getTableReportByClassesId($classes_id)
    {
        $coursetableQuery = db::name("fa_course_table")
            ->where("classes_id", "like", "%," . $classes_id . ",%")
            ->whereOr("classes_id", "like", "%," . $classes_id)
            ->whereOr("classes_id", "like", $classes_id . ",%")
            ->whereOr("classes_id", "=", $classes_id)
            ->find();
        $table_id=$coursetableQuery["id"];
        $apply_form=db::name("eb_apply_form")->where("table_id","=", $table_id)->find();
        if($apply_form){
            $apply_form_id=$apply_form["id"];
        }
        return [
            "apply_form_id"=>$apply_form_id,
            "table_id"=>$table_id,
        ];
    }
}